/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import main.WinkelApplication;
import model.User;

/**
 *
 * @author Jesse
 */
public class LogInScreen extends JPanel implements MouseListener, ActionListener {
    private JTextField tfUser;
    private JPasswordField tfPass;
    private JLabel fail;
    
    public LogInScreen(){
        super();
        setLayout(null);
        initComponents();
    }

    private void initComponents() {
        JLabel lblUser = new JLabel("Username:");
        lblUser.setBounds(20, 20, 100, 20);
        add(lblUser);

        tfUser = new JTextField();
        tfUser.setBounds(140, 20, 150, 20);
        add(tfUser);
        
        JLabel lblPass = new JLabel("Password:");
        lblPass.setBounds(20, 60, 100, 20);
        add(lblPass);

        tfPass = new JPasswordField();
        tfPass.setBounds(140, 60, 150, 20);
        add(tfPass);

        JButton btnSend = new JButton("Login");
        btnSend.setName("1");
        btnSend.setBounds(20, 100, 100, 20);
        btnSend.addActionListener(this);
        this.add(btnSend);
        
        JButton debug = new JButton("Debug");
        debug.setName("2");
        debug.isSelected();
        debug.setBounds(140, 100, 100, 20);
        debug.addActionListener(this);
        this.add(debug);
        
        fail = new JLabel("Failed to log in");
        fail.setBounds(20, 140, 200, 20);
        fail.setForeground(Color.red);
    }
    
    
    public void mouseClicked(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void actionPerformed(ActionEvent ae) 
    {
        JButton button = (JButton) ae.getSource();
        
        // Login button
        if (button.getName().equals("1")) 
        {
            String username = tfUser.getText();
            String password = tfPass.getText();
            User currentUser = WinkelApplication.getQueryManager().getUser(username);
            if (currentUser != null && currentUser.getPassword().equals(password))
            {
                WinkelApplication.getInstance().setAccesLevel(currentUser.getAccesLevel());
                if(currentUser.getAccesLevel()==2) {WinkelApplication.getInstance().showPanel(new view.supply.SupplyPanel());}
                else {WinkelApplication.getInstance().showPanel(new view.CategoryList());}
            } 
            else
            {
                add(fail);
                repaint();
            }
        } 
        // Debug button
        else if (button.getName().equals("2")) 
        {
            WinkelApplication.getInstance().setAccesLevel(3);
            WinkelApplication.getInstance().showPanel(new view.CategoryList());
        }
    }

    
}
